﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using IESN.BaseForms;

namespace IESN
{
    /// <summary>
    /// 整流机信息设置窗体
    /// </summary>
    public partial class RectifierMonitorFrm : EditBaseFrm
    {
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public RectifierMonitorFrm()
        {
            InitializeComponent();
            CurrentEntity = new Rect();
        }

          /// <summary>
        /// 构造函数
        /// </summary>
        public RectifierMonitorFrm(AppContext appContext)
            :this()
        {
            _appContext = appContext;
        }


        #endregion
        
        #region 行切换

        private void dgvRect_SelectionChanged(object sender, EventArgs e)
        {
            CommonHelper<Rect> cHelper = new CommonHelper<Rect>();

            if (dgvRect.SelectedRows != null && dgvRect.SelectedRows.Count > 0)
            {
                DataGridViewRow gridrow = dgvRect.SelectedRows[0];
                DataRowView row_view = (DataRowView)gridrow.DataBoundItem;
                CurrentEntity = cHelper.FillModel(row_view.Row);

                ControlHelper.SetObjectValueToControl(CurrentEntity, panelContent);
            }
        }

        #endregion

        #region 操作完成后处理

        /// <summary>
        /// 新增后处理
        /// </summary>
        protected override void AfterInserted()
        {
            base.AfterInserted();
            InsertGrid();
        }

        /// <summary>
        /// 编辑后处理
        /// </summary>
        protected override void AfterUpDated()
        {
            base.AfterUpDated();
            UpdateGrid();
        }

        /// <summary>
        /// 插入到DataGrid
        /// </summary>
        private void InsertGrid()
        {
            BindingSource bs = dgvRect.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;

            CommonHelper<Rect> cHelper = new CommonHelper<Rect>();
            DataRow newDr = cHelper.FillDataRow(CurrentEntity as Rect, dt);
            DataTable cloneTb = dt.Copy();

            cloneTb.Rows.Add(newDr.ItemArray);

            BindingSource newBS = new BindingSource();
            newBS.DataSource = cloneTb;
            dgvRect.DataSource = newBS;

            dgvRect.Rows[cloneTb.Rows.Count - 1].Selected = true;
            dgvRect.Refresh();
        }

        /// <summary>
        /// 更新到DataGrid
        /// </summary>
        private void UpdateGrid()
        {
            BindingSource bs = dgvRect.DataSource as BindingSource;
            DataTable dt = bs.DataSource as DataTable;
            DataRow[] drs = dt.Select("ID='" + CurrentEntity.ID + "'");

            if (drs != null && drs.Length > 0)
            {
                DataRow dr = drs[0];

                CommonHelper<Rect> cHelper = new CommonHelper<Rect>();
                DataRow newDr = cHelper.FillDataRow(CurrentEntity as Rect, dt);

                drs[0].ItemArray = newDr.ItemArray;
                dgvRect.Refresh();
            }
        }

        #endregion
    }
}
